package de.herberlin.webapp.user;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.herberlin.webapp.core.Functions;

public class AdminFilter implements Filter {

	private Log logger=LogFactory.getLog(getClass());
	
	public void destroy() {

	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req=(HttpServletRequest)arg0;
		HttpServletResponse resp=(HttpServletResponse)arg1;
		
		if (Functions.isAdmin(req)) {
			chain.doFilter(arg0, arg1);
		} else {
			logger.debug("User  not loggend in");
			resp.sendRedirect(resp.encodeRedirectURL(req.getContextPath()+"/login.jsp"));
			// req.getRequestDispatcher("/login.jsp").include(req, arg1);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {

	}

}
